package com.sgcc.activiti.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.activiti.domain.PendingLeaderboards;
import com.sgcc.activiti.domain.vo.DateSlot;
import com.sgcc.common.datascope.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.activiti.domain.OAnomalyStat;
import com.sgcc.activiti.service.IOAnomalyStatService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 看板统计 Controller
 *
 * @author sgcc
 * @date 2021-04-07
 */
@RestController
@RequestMapping("/anomalystat")
public class OAnomalyStatController extends BaseController {
    @Autowired
    private IOAnomalyStatService oAnomalyStatService;

    /**
     * 查询看板统计 列表
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:list")
    @GetMapping("/list")
    public TableDataInfo list(OAnomalyStat oAnomalyStat) {
        startPage();
        List<OAnomalyStat> list = oAnomalyStatService.selectOAnomalyStatList(oAnomalyStat);
        return getDataTable(list);
    }

    /**
     * 导出看板统计 列表
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:export")
    @Log(title = "看板统计 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OAnomalyStat oAnomalyStat) throws IOException {
        List<OAnomalyStat> list = oAnomalyStatService.selectOAnomalyStatList(oAnomalyStat);
        ExcelUtil<OAnomalyStat> util = new ExcelUtil<OAnomalyStat>(OAnomalyStat.class);
        util.exportExcel(response, list, "anomalystat");
    }

    /**
     * 获取看板统计 详细信息
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:query")
    @GetMapping(value = "/{statDate}")
    public AjaxResult getInfo(@PathVariable("statDate") String statDate) {
        return AjaxResult.success(oAnomalyStatService.selectOAnomalyStatById(statDate));
    }

    /**
     * 新增看板统计
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:add")
    @Log(title = "看板统计 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OAnomalyStat oAnomalyStat) {
        return toAjax(oAnomalyStatService.insertOAnomalyStat(oAnomalyStat));
    }

    /**
     * 修改看板统计
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:edit")
    @Log(title = "看板统计 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OAnomalyStat oAnomalyStat) {
        return toAjax(oAnomalyStatService.updateOAnomalyStat(oAnomalyStat));
    }

    /**
     * 删除看板统计
     */
    //@PreAuthorize(hasPermi = "activiti:anomalystat:remove")
    @Log(title = "看板统计 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{statDates}")
    public AjaxResult remove(@PathVariable String[] statDates) {
        return toAjax(oAnomalyStatService.deleteOAnomalyStatByIds(statDates));
    }

    @GetMapping("monitorCount")
    @DataScope(deptAlias = "sys_dept")
    public AjaxResult getMonitorCount(PendingLeaderboards pendingLeaderboards) {
        Map map = oAnomalyStatService.getMonitorCount(pendingLeaderboards);
        return AjaxResult.success(map);
    }

    /**
     * 运营监控看板，系统监控分布
     */
    @GetMapping("systemMonitoring")
    public AjaxResult getSystemMonitoring() {
        List<HashMap<String, String>> mapList = oAnomalyStatService.getSystemMonitoring();
        return AjaxResult.success(mapList);
    }

    /**
     * 运营监控看板，监控类型分布
     */
    @GetMapping("monitoringType")
    public AjaxResult monitoringType() {
        List<HashMap<String, String>> mapList = oAnomalyStatService.monitoringType();
        return AjaxResult.success(mapList);
    }

    /**
     * 运营监控看板，待处理异常数
     */

    @GetMapping("pendingException")
    @DataScope(deptAlias = "sys_dept")
    public AjaxResult pendingException(PendingLeaderboards pendingLeaderboards) {
        List<HashMap<String, String>> map = oAnomalyStatService.pendingException(pendingLeaderboards);
        return AjaxResult.success(map);
    }

    /**
     * 运营监控看板，待处理排行榜
     */
    @DataScope(deptAlias = "sys_dept")
    @GetMapping("/pendingLeaderboards")
    public TableDataInfo pendingLeaderboards(PendingLeaderboards pendingLeaderboards) {
        startPage();
        List<PendingLeaderboards> list = oAnomalyStatService.pendingLeaderboards(pendingLeaderboards);
        return getDataTable(list);
    }

    /**
     * 关键业务监控-各类系统占比
     *
     * @return
     */
    @GetMapping(value = "getPendingSystem")
    public AjaxResult getPendingSystem(String monsyscode, String monsystype) {
        //	String monsyscode = "01,02,03,05";
        System.out.println(monsyscode);
        List<HashMap<String, String>> ss = oAnomalyStatService.getPendingSystem(monsyscode, monsystype);
        return AjaxResult.success(ss);
    }

    /**
     * 键业务监控-各类系统占比柱状图
     *
     * @param monsyscode 监控系统
     * @param monsystype 监控类型
     * @param alltime    开始+结束时间
     * @return
     */
    @GetMapping(value = "getPendingSystemforhistogram")
    public AjaxResult getPendingSystemforhistogram(String monsyscode, String monsystype, String alltime, String dt) throws ParseException {
        //	String monsyscode = "01,02,03,05";
        System.out.println(monsyscode);
        List<HashMap<String, Object>> ss = oAnomalyStatService.getPendingSystemforhistogram(monsyscode, monsystype, alltime, dt);
        return AjaxResult.success(ss);
    }

    /**
     * 数据库及主机监控-监控类型分布
     *
     * @param monsystype
     * @return
     */
    @GetMapping(value = "getMonTypePercentage")
    public AjaxResult getMonTypePercentage(String monsystype) {
        //	String monsyscode = "01,02,03,05";
        System.out.println(monsystype);
        List<HashMap<String, Object>> ss = oAnomalyStatService.getMonTypePercentage(monsystype);
        return AjaxResult.success(ss);
    }


    /**
     * 运营监控看板，系统异常数排名
     */
    @GetMapping(value = "sysAbnormalRank")
    public AjaxResult getSysAbnormalRank(DateSlot dateSlot) {
        List<HashMap<String, String>> list = oAnomalyStatService.getSysAbnormalRank(dateSlot);
        return AjaxResult.success(list);
    }

    /**
     * 运营监控看板，监控异常趋势(柱状图)
     */
    @GetMapping(value = "sysAbnormalTrend")
    public AjaxResult getSysAbnormalTrend(String type, DateSlot dateSlot) {
        List<HashMap<String, String>> list = oAnomalyStatService.getSysAbnormalTrend(type, dateSlot);
        return AjaxResult.success(list);
    }


    /**
     * 定时任务
     */
    @GetMapping(value = "updateOAnomalyStatTask")
    public AjaxResult updateOAnomalyStat() {
        oAnomalyStatService.updateOAnomalyStatTask();
        return AjaxResult.success();
    }
}
